﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YinLong.TestWinform
{
    // 运算基类（抽象产品类）：
    // --------------------
    public class Operation
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    // 运算符派生类（具体产品类）：
     // ------------------------
    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
                throw new Exception("除数不可为 0");
            result = NumberA / NumberB;
            return result;
        }
    }
    public class OperationFactory
    {
        public static Operation CreateOperate(string operate)
        {
            Operation a = new OperationAdd();
            //根据传入的参数operate选择实例化的类
            Operation oper = null;
            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }
    }
}
